package io.comparison.service.impl;

import io.comparison.dao.DBProdDao;
import io.comparison.dao.DBTestDao;
import io.comparison.service.ComparisonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ComparisonServiceImpl implements ComparisonService {

    @Autowired
    private DBTestDao dbTestDao;
    @Autowired
    private DBProdDao dbProdDao;

    @Override
    public List<Map<String, Object>> queryList(Map<String, Object> params) {
        List<Map<String, Object>> testList = dbTestDao.queryList(params);
        List<Map<String, Object>> prodList = dbProdDao.queryList(params);
        // 筛选出一样的表(测试环境与正式环境比较)
        List<Map<String, Object>> sameList = new ArrayList<>();
        testList.stream().forEach(testTable -> {
            prodList.stream().forEach(prodTable -> {
                if (testTable.get("tableName").equals(prodTable.get("tableName"))) {
                    sameList.add(testTable);
                }
            });
        });
        List<Map<String, Object>> result = new ArrayList<>();
        List<Map<String, Object>> result1 = new ArrayList<>();

        // 查看这些表字段是否相同(测试环境与正式环境比较)
        sameList.stream().forEach(table -> {
            List<Map<String, Object>> testColumns = dbTestDao.queryColumns(table);
            List<Map<String, Object>> prodColumns = dbProdDao.queryColumns(table);
            Boolean flag = compareColumns(testColumns, prodColumns);
            table.put("sameFlag", flag);
            if (!flag)
                result.add(table);
            else
                result1.add(table);
        });
        result.addAll(result1);
        return result;
    }

    @Override
    public List<Map<String, Object>> queryTestList(Map<String, Object> params) {
        List<Map<String, Object>> testList = dbTestDao.queryList(params);
        List<Map<String, Object>> prodList = dbProdDao.queryList(params);
        // 筛选出测试库独有的表(测试环境与正式环境比较)
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map<String, Object> testTable : testList) {
            boolean flag = false;
            for (Map<String, Object> prodTable : prodList) {
                if (testTable.get("tableName").equals(prodTable.get("tableName"))) {
                    flag = true;
                    break;
                }
            }
            // 如果表在prodList中找不到表名一样的，则表示这个表测试库独有
            if (!flag)
                list.add(testTable);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> queryProdList(Map<String, Object> params) {
        List<Map<String, Object>> testList = dbTestDao.queryList(params);
        List<Map<String, Object>> prodList = dbProdDao.queryList(params);
        // 筛选出测试库独有的表(测试环境与正式环境比较)
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map<String, Object> prodTable : prodList) {
            boolean flag = false;
            for (Map<String, Object> testTable : testList) {
                if (testTable.get("tableName").equals(prodTable.get("tableName"))) {
                    flag = true;
                    break;
                }
            }
            // 如果表在testList中找不到表名一样的，则表示这个表测试库独有
            if (!flag)
                list.add(prodTable);
        }
        return list;
    }

    @Override
    public Map<String, Object> getTable(Map<String, Object> params) {
        Map<String, Object> maps = new HashMap<>();
        List<Map<String, Object>> testList = dbTestDao.queryColumns(params);
        List<Map<String, Object>> prodList = dbProdDao.queryColumns(params);
        for (Map<String, Object> prodTable : prodList) {
            boolean flag = false;
            for (Map<String, Object> testTable : testList) {
                if (testTable.get("columnName").equals(prodTable.get("columnName"))) {
                    flag = true;
                    break;
                }
            }
            prodTable.put("same", flag);
        }
        for (Map<String, Object> testTable : testList) {
            boolean flag = false;
            for (Map<String, Object> prodTable : prodList) {
                if (testTable.get("columnName").equals(prodTable.get("columnName"))) {
                    flag = true;
                    break;
                }
            }
            testTable.put("same", flag);
        }
        maps.put("test", testList);
        maps.put("prod", prodList);
        return maps;
    }

    /**
     * 比较两个表字段是否全部相同
     *
     * @param testColumns
     * @param prodColumns
     * @return
     */
    private Boolean compareColumns(List<Map<String, Object>> testColumns, List<Map<String, Object>> prodColumns) {
        for (Map<String, Object> testColumn : testColumns) {
            boolean flag = false;
            for (Map<String, Object> prodColumn : prodColumns) {
                // 字段名和字段类型都一样
                if (testColumn.get("columnName").equals(prodColumn.get("columnName")) // 字段名
                        && testColumn.get("dataType").equals(prodColumn.get("dataType"))// 数据类型
//                        && testColumn.get("characterMaximumLength").equals(prodColumn.get("characterMaximumLength")) // 字符长度
//                        && testColumn.get("isNullable").equals(prodColumn.get("isNullable"))// 是否为空
//                        && testColumn.get("columnComment").equals(prodColumn.get("columnComment"))// 备注
//                        && testColumn.get("columnKey").equals(prodColumn.get("columnKey"))// 主键字段
                ) {
                    flag = true;
                    break;
                }
            }
            // 如果字段在prodColumns中找不到相同字段,则表明两个表字段不一致
            if (!flag) {
                return false;
            }
        }
        return true;
    }
}
